﻿using System.Collections;
using UnityEngine;
public class PlayerStateController: ControllerBase
{
    
    //冲刺
    public float dashDir = 1;//冲刺方向
    
    public float moveX;
    public float moveY;
    
    //comboInitXy
    [SerializeField]public Vector2[] comboXXVelocity;

    protected override void InitStats()
    {
        base.InitStats();
        characterBase.InitStats(StatType.MaxHealth,Setting.health);
        characterBase.InitStats(StatType.CurrentHealth,Setting.health);
        characterBase.InitStats(StatType.Damage,Setting.attackDamage);
        characterBase.InitStats(StatType.CurDefense,Setting.defense);
        characterBase.InitStats(StatType.MaxDefense,Setting.defense);
        characterBase.InitStats(StatType.CritRate,Setting.criRate);
        characterBase.InitStats(StatType.CritDamage,Setting.criDamage);
        characterBase.InitStats(StatType.DodgeRate,Setting.dogeRate);
        
        characterBase.InitStats(StatType.MagicResistance,Setting.magicResistance);
        characterBase.InitStats(StatType.FireDamage,Setting.fireDamge);
        characterBase.InitStats(StatType.FireCumulativeValue,0);
        characterBase.InitStats(StatType.IceDamage,Setting.iceDamge);
        characterBase.InitStats(StatType.IceCumulativeValue,0);
        characterBase.InitStats(StatType.ElectricDamage,Setting.electricDamge);
        characterBase.InitStats(StatType.ElectricCumulativeValue,99);
    }

    protected override void InitStates()
    {
        base.InitStates();
        stateMachine.AddState(new PlayerIdleState(this, stateMachine,
    Setting.idle,Setting.AnimatorIdle));
        stateMachine.AddState(new PlayerMoveState(this, stateMachine,
            Setting.move, Setting.AnimatorMove));
        stateMachine.AddState(new PlayerDashState(this, stateMachine,
            Setting.dash, Setting.AnimatorDash));
        stateMachine.AddState(new PlayerJumpState(this, stateMachine,
            Setting.jump, Setting.AnimatorJump));
        stateMachine.AddState(new PlayerAirState(this, stateMachine,
            Setting.air, Setting.AnimatorJump));
        stateMachine.AddState(new PlayerWallSlide(this, stateMachine,
            Setting.wallSlide, Setting.AnimatorWallSlide));
        stateMachine.AddState(new PlayerWallJump(this, stateMachine,
            Setting.wallJump, Setting.AnimatorJump));
        stateMachine.AddState(new PlayerPrimaryAttack(this,stateMachine,
            Setting.primaryAttack, Setting.AnimatorAttack));
        stateMachine.AddState(new PlayerCounterState(this,stateMachine,
            Setting.counter, Setting.AnimatorCounter));
        stateMachine.AddState(new PlayerSwordAimState(this,stateMachine,
            Setting.swordAim, Setting.AnimatorSwordAim));
        stateMachine.AddState(new PlayerSwordCatchState(this,stateMachine,
            Setting.swordCatch, Setting.AnimatorSwordCatch));
        stateMachine.AddState(new PlayerBackHoleState(this,stateMachine,
            Setting.backHole, Setting.AnimatorJump));
        stateMachine.AddState(new PlayerDeadState(this,stateMachine,
            Setting.dead, Setting.AnimatorDead));
    }

    protected override void Update()
    {
        moveX = Input.GetAxisRaw("Horizontal");
        moveY = Input.GetAxisRaw("Vertical");
        
        // 冲刺输入
        if (Input.GetKeyDown(KeyCode.LeftShift))
        {
            DashInput();
        }
        if (Input.GetKeyDown(KeyCode.E))
        {
            CrystalInput();
        }
        base.Update();
    }

    #region GlobalInput
    
    private void DashInput()
    {
        //当黑洞技能发动时候 无输入
        if (SkillManager.Instance.IsSkillInUse(Setting.curHoleSkill))
        {
            return;
        }
        //如果贴着墙，不进行冲刺
        if (isWall) return;
        
        if (SkillManager.Instance
                .GetSkill<DashSkill>(Setting.curDashSkill).CanUseSkill())
        {
            dashDir = moveX;
            if (dashDir==0)//冲刺方向为0时，使用面朝方向
            {
                dashDir = faceDir;
            }
            stateMachine.ChangeState(Setting.dash);
        }
    }

    private void CrystalInput()
    {
        //当黑洞技能发动时候 无输入
        if (SkillManager.Instance.IsSkillInUse(Setting.curHoleSkill))
        {
            return;
        }
        var skill = SkillManager.Instance.GetSkill<CrystalSkill>(Setting.curCrystalSkill);
        //查看是否存在水晶
        if (skill.hasCrystal)
        {
            //触发水晶技能
            EventCenter.Instance.EventTrigger(E_EventType.Crystal_Trigger);
        }
        else
        {
            if (skill.CanUseSkill())
            {
                //创造水晶
                SkillManager.Instance.CreateClone<CrystalSkill>
                ("Crystal",transform.position
                    ,faceDir,Setting.curCrystalSkill);
            }
        }
    }

    #endregion

    protected override void OnDrawGizmos()
    {
        base.OnDrawGizmos();
        if (attackCheck!=null)
        {
            Gizmos.color = Color.yellow;
            Gizmos.DrawWireSphere(attackCheck.position, Setting.attackRadius);
            
        }
    }
}